package com.sdy.sd;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;

public class AniFirework extends View {
    private static final String TAG = "AniFirework";
    private Paint mPaint;
    private ArrayList<Bitmap> mBmps;
    private float mBaseLeft = 0;
    private float mBaseTop = 0;
    public int mStatus = ST_NA;
    private int mCurFrmIdx = 0;
    private int DELAY = 100;
    long mLastTick = 0;

    static final int ST_NA          = -1;
    static final int ST_STOPPED     = 0;
    static final int ST_PLAYING     = 1;

    public AniFirework(Context context) {
        super(context);
        asyncInitRes();
    }
    public AniFirework(Context context, AttributeSet attrs) { // in using
        super(context, attrs);
        asyncInitRes();
    }
    private void asyncInitRes() {
        new Thread(new Runnable() { public void run() {
            initRes();
        }}).start();
    }

    private void initRes() {
        mBmps = new ArrayList<Bitmap>();
        String pkgName = getContext().getPackageName();
        Resources res = getContext().getResources();
        for(int i = 0; i < 19; ++i) {
            int resId = getResources().getIdentifier("spark" + i, "drawable", pkgName);
            Bitmap bmp = BitmapFactory.decodeResource(res, resId);
            assert (bmp != null);
            mBmps.add(filterColor(bmp, 0xFF000000));
        }

        DisplayMetrics dm = getResources().getDisplayMetrics();
        mBaseLeft = dm.widthPixels / 2 - mBmps.get(0).getWidth() / 2;
        mBaseTop = 10;
        mPaint = new Paint(); //Paint.ANTI_ALIAS_FLAG
        mStatus = ST_STOPPED;
    }
    private Bitmap filterColor(Bitmap bmp, int fltColor) {
        Bitmap bmpCopy = bmp.copy(Bitmap.Config.ARGB_8888 ,true);
        int w = bmpCopy.getWidth();
        int h = bmpCopy.getHeight();
        int noA = fltColor & ~0xFF000000;
        int color = 0;

        int r, g, b;
        for (int px = 0; px < w; px++){
            for (int py = 0; py < h; py++) {
                color = bmpCopy.getPixel(px,py);
                if (noA == (color & 0xFFFFFF)) {
                    bmpCopy.setPixel(px, py, 0x00000000);
                    continue;
                }
//                r = (0xFF - ((color & 0xFF0000) >> 16));
//                g = (0xFF - ((color & 0xFF00) >> 8));
//                b = (0xFF - (color & 0xFF));
//                bmpCopy.setPixel(px, py, Color.argb(0xA0, r, g, b));
            }
        }
        return bmpCopy;
    }
    protected void onDraw(Canvas canvas) {
        //Bitmap newBlankBmp = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, Bitmap.Config.ARGB_8888);
        if(ST_PLAYING != mStatus) return;
        if(mCurFrmIdx >= 19) {
            mCurFrmIdx = 0;
            mStatus = ST_STOPPED;
            return;
        }
        long time = (System.currentTimeMillis() - mLastTick);

        if (time >= DELAY) {
            mLastTick = System.currentTimeMillis();
            Bitmap bmp = mBmps.get(mCurFrmIdx++);
            canvas.drawBitmap(bmp, mBaseLeft, mBaseTop, mPaint);
            invalidate();
            return;
        }
        Bitmap bmp = mBmps.get(mCurFrmIdx);
        canvas.drawBitmap(bmp, mBaseLeft, mBaseTop, mPaint);
        invalidate();
    }
    public int startAni() {
        if(ST_NA == mStatus) return -1;
        mCurFrmIdx = 0;
        mStatus = ST_PLAYING;
        invalidate();
        return 0;
    }
}
